<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item>
        <el-input v-model="queryParams.taskNo" placeholder="请输入订单编号" clearable size="small"
                  @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-input v-model="queryParams.proNo" placeholder="请输入物品编号" clearable size="small"
                  @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-select v-model="queryParams.status" placeholder="请选择状态">
          <el-option
            v-for="dict in dict.type.wms_stock_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-date-picker v-model="daterange" size="small" style="width: 240px" value-format="yyyy-MM-dd"
                        type="daterange" range-separator="-" start-placeholder="暂存开始时间" end-placeholder="截止日期"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['wms:deal:export']"
        >导出
        </el-button>
      </el-form-item>
    </el-form>
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button :disabled="this.multiple" type="primary" plain icon="el-icon-plus" size="mini"
                   v-has-permi="['oms:disposal:tempStorage']" @click="handleTempStorage"
        >暂存
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="tableList" @selection-change="handleSelectionChange" border stripe>
      <el-table-column type="selection" width="55" align="center" :selectable="selectable"/>
      <el-table-column label="序号" type="index" width="50" align="center"/>
      <el-table-column label="订单编号" align="center" prop="taskNo"/>
      <el-table-column label="物品编号" align="center" prop="pno" width="250"/>
      <el-table-column label="物品名称" align="center" prop="name"/>
      <el-table-column label="仓库" align="center" prop="storeName"/>
      <el-table-column label="库区" align="center" prop="areaName"/>
      <el-table-column label="存放位置" align="center">
        <template slot-scope="scope">
          {{ scope.row.groupName ? scope.row.groupName + '/' : '' }}
          {{ scope.row.cabinetName ? scope.row.cabinetName + '/' : '' }}
          {{ scope.row.locatorName ? scope.row.locatorName : '' }}
        </template>
      </el-table-column>
      <el-table-column label="状态" align="center">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.wms_stock_status" :value="scope.row.status"/>
        </template>
      </el-table-column>
      <el-table-column label="暂存时间" align="center" prop="tempStorageTime"/>
      <el-table-column label="下单时间" align="center" prop="createTime"/>
      <el-table-column label="操作" class-name="small-padding fixed-width" width="80" align="center" fixed="right">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-circle-close" @click="handleCancelTempStorage(scope.row)"
                 v-if="scope.row.status ==='9'"    v-hasPermi="['oms:disposal:cancelTempStorage']">取消</el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
                @pagination="getList"
    />
  </div>
</template>

<script>
import { cancelDisposalTempStorage, disposalTempStorage, getTempStorageList } from '@/api/oms/dealTask'

export default {
  dicts: ['wms_stock_status'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      tableList: [],
      daterange: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        status: '0'
      },
      // 表单参数
      form: {}
    }
  },
  created() {
    this.getList()
  },
  methods: {
    handleExport() {
      this.download('/oms/task/disposal/exportTempStorageList', {
        ...this.queryParams
      }, `disposal_${new Date().getTime()}.xlsx`)
    },
    getList() {
      this.loading = true
      getTempStorageList(this.addDateRange(this.queryParams, this.daterange)).then(response => {
        this.tableList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    selectable(row){
      return row.status ==='0'
    },
    // 表单重置
    reset() {
      this.form = {}
      this.resetForm('form')
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
      this.queryParams.status = '0'
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    handleCancelTempStorage(row){
      this.$modal.confirm(`是否确认取消暂存？`).then(() => {
        this.$modal.loading('取消中....')
        cancelDisposalTempStorage([row.id]).then(() => {
          this.handleQuery()
        }).finally(() => {
          this.$modal.closeLoading()
        })
      })
    },
    handleTempStorage() {
      this.$modal.confirm(`是否确认暂存${this.ids.length}个物品？`).then(() => {
        this.$modal.loading('暂存中....')
        disposalTempStorage(this.ids).then(() => {
          this.handleQuery()
        }).finally(() => {
          this.$modal.closeLoading()
        })
      })
    }
  }
}
</script>
